package com.tinycomment.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tinycomment.dbutils.Parameter;
import com.tinycomment.dbutils.SqlHelper;
import com.tinycomment.idao.IExample;
import com.tinycomment.model.Example;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-6-22 
 */
public class ExampleDao implements IExample{

	@Override
	public boolean Add(Example model) {
		String strSql = "INSERT INTO tiny_example(name,description)VALUES(?,?)";
		Parameter[] paras = {
				new Parameter(1, String.class, model.getName()),
				new Parameter(2, String.class, model.getDescription()) };
		SqlHelper helper = SqlHelper.getInstance();
		boolean result = helper.ExecuteSql(strSql, paras);
		return result;
	}

	@Override
	public boolean Update(Example model) {
		String strSql = "UPDATE tiny_example SET name=?,description=? WHERE  exampleid=?";
		Parameter[] paras = {
				new Parameter(1, String.class, model.getName()),
				new Parameter(2, String.class, model.getDescription()),
				new Parameter(3, Integer.class, model.getExampleID()) };
    
		SqlHelper helper = SqlHelper.getInstance();
		return helper.ExecuteSql(strSql, paras);
	}

	@Override
	public boolean Delete(int id) {
		String strSql = "DELETE FROM tiny_example WHERE exampleid=?";
        Parameter[] paras = {
                        new Parameter(1,Integer.class,id)        
                    };
        SqlHelper helper = SqlHelper.getInstance();
        return helper.ExecuteSql(strSql,paras);
	}

	@Override
	public Example getModel(int id) {
		String strSql = "SELECT exampleid,name,description FROM tiny_example WHERE exampleid=?";
        Parameter[] paras = {
                        new Parameter(1,Integer.class,id)
                    };
        ResultSet rs = null;
        SqlHelper helper = SqlHelper.getInstance();
        rs = helper.getResultSet(strSql,paras);
        Example mod = null;
        try {
            if(rs.next()){
                mod = new Example();
                mod.setExampleID(rs.getInt("exampleid"));
                mod.setName(rs.getString("name"));
                mod.setDescription(rs.getString("description"));
            }
            rs.close();
            helper.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return mod;
	}

	@Override
	public List<Example> getModelSet(String condition, Parameter[] paras) {
		String strSql = "SELECT exampleid,name,description,caseid FROM tiny_example WHERE "+condition;
        ResultSet rs = null;
        SqlHelper helper = SqlHelper.getInstance();
        rs = helper.getResultSet(strSql,paras);
        List<Example> list = new ArrayList<Example>();
        try {
            while(rs.next()){
            	Example mod = new Example();
                mod.setExampleID(rs.getInt("exampleid"));
                mod.setName(rs.getString("name"));
                mod.setDescription(rs.getString("description"));
                list.add(mod);
            }
            rs.close();
            helper.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
	}

}
